package com.psfd.demo.controller;

import com.psfd.demo.bean.Customer;
import com.psfd.demo.service.CustomerService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/cusinsert")
    public ModelAndView insert(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cusadd");
        return modelAndView;
    }


    @RequestMapping("/insertCus")
    public ModelAndView add(Customer customer){
        System.err.println(customer+"1111111111111");
        customerService.save(customer);
        return queryAll();
    }




    @RequestMapping("/cusupdate")
    public ModelAndView cusupdate(Integer cusid){
        System.out.println(cusid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cusid",cusid);
        modelAndView.setViewName("cusupdate");
        return modelAndView;
    }

    @RequestMapping("/updateCus")
    public ModelAndView updateCus(Customer customer){
        System.err.println(customer+"--------------------");
        customerService.updateById(customer);
        return queryAll();
    }

    @RequestMapping("/deleteCus")
    public ModelAndView delete(Integer cusid){
        System.err.println(cusid+"....................");
        customerService.removeById(cusid);
        return queryAll();
    }


    @RequestMapping("/cusQueryAll")
    public ModelAndView queryAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cuslist",customerService.list());
        modelAndView.setViewName("cusList");
        return modelAndView;
    }


//    @RequestMapping("/cusreturnpage")
//    public ModelAndView returnpage(){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("homepage");
//        return modelAndView;
//    }
}
